﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightMap.LocationServiceReference;
using System.Windows.Media.Imaging;
using Microsoft.Maps.MapControl;
using System.Diagnostics;

namespace SilverlightMap
{
    public partial class MainPage : UserControl
    {
        private double _latitude;
        private double _longtitude;

        public MainPage()
        {
            InitializeComponent();
            this.Loaded +=new RoutedEventHandler(MainPage_Loaded);
        }

        public void MainPage_Loaded(object sender, EventArgs args)
        {
            LocationServiceClient locationClient = new LocationServiceClient();
           
            locationClient.GetUserLocationCompleted += new EventHandler<GetUserLocationCompletedEventArgs>(locationClient_GetUserLocationCompleted);
            locationClient.GetUserLocationAsync(((App)Application.Current).Username);
        }

        public void locationClient_GetUserLocationCompleted(object sender, GetUserLocationCompletedEventArgs args)
        {
            int defaultZoom = 7;
            SilverlightMap.LocationServiceReference.Location location = args.Result;
            myMap.SetView(new Microsoft.Maps.MapControl.Location(location.Latitude, location.Longitude), defaultZoom);
            AddPushpin(location.Latitude, location.Longitude);
            _latitude = location.Latitude;
            _longtitude = location.Longitude;

            SilverlightMap.AlertsService.GlobalAlertsServiceClient alertClient = new SilverlightMap.AlertsService.GlobalAlertsServiceClient();

            alertClient.GetAlertsCompleted += new EventHandler<SilverlightMap.AlertsService.GetAlertsCompletedEventArgs>(alertClient_GetAlertsCompleted);
            alertClient.GetAlertsAsync(((App)Application.Current).Username);
        }

        void alertClient_GetAlertsCompleted(object sender, SilverlightMap.AlertsService.GetAlertsCompletedEventArgs args)
        {
            Canvas notificationCanvas = new Canvas();
            notificationCanvas.Width = 50;
            notificationCanvas.Height = 50;
            notificationCanvas.Opacity = 0.7;
            notificationCanvas.SetValue(Microsoft.Maps.MapControl.MapLayer.PositionProperty, new Microsoft.Maps.MapControl.Location(_latitude, _longtitude));
            notificationCanvas.SetValue(Microsoft.Maps.MapControl.MapLayer.PositionOriginProperty, PositionOrigin.BottomCenter);
            Path notifPath = new Path();
            notifPath.Data = CreateGeometry();
            notifPath.Fill = new SolidColorBrush(Colors.Red);
            notifPath.Stroke = new SolidColorBrush(Colors.Orange);
            notifPath.StrokeThickness = 2;
            TextBlock notificationBlock = new TextBlock();
            notificationBlock.FontSize = 10;
            notificationBlock.Foreground = new SolidColorBrush(Colors.White);
            notificationBlock.Margin = new Thickness(5);
            notificationBlock.TextAlignment = TextAlignment.Center;
            notificationBlock.Text = args.Result.FirstOrDefault().NotificationDescription.notification;
            notificationCanvas.Children.Add(notifPath);
            notificationCanvas.Children.Add(notificationBlock);
            myMap.Children.Add(notificationCanvas);
        }

        private Geometry CreateGeometry()
        {
            //"M 0,0 L 50,0 50,50 25,80 0,50 0,0"
            RectangleGeometry blackRectGeometry = new RectangleGeometry();
            Rect rct = new Rect();
            rct.X = 0;
            rct.Y = 0;
            rct.Width = 150;
            rct.Height = 30;
            blackRectGeometry.Rect = rct;
            return blackRectGeometry;
        }

        private void AddPushpin(double latitude, double longitude)
        {
            Pushpin pushpin = new Pushpin();
            pushpin.Location = new Microsoft.Maps.MapControl.Location(latitude, longitude);
            myMap.Children.Add(pushpin);
        }
    }
}
